/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rpgmapper.gui.effects;

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.effects.EffectImpl;
import de.lessvoid.nifty.effects.EffectProperties;
import de.lessvoid.nifty.effects.Falloff;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.render.NiftyRenderEngine;
import de.lessvoid.nifty.tools.Alpha;
import de.lessvoid.nifty.tools.Color;
import de.lessvoid.nifty.tools.LinearInterpolator;
import de.lessvoid.nifty.tools.SizeValue;
import de.lessvoid.nifty.tools.pulsate.Pulsator;

/**
 * Color - color overlay.
 * @author void
 */
public class AlphaPulsate implements EffectImpl {

    private Alpha start = Alpha.ZERO;
    private Alpha end = Alpha.FULL;
    private LinearInterpolator interpolator;
    private Pulsator pulsator;
    private boolean activated = false;

    public void activate(final Nifty nifty, final Element element, final EffectProperties parameter) {
        if (parameter.getProperty("start") != null) {
            start = new Alpha(parameter.getProperty("start"));
        }
        if (parameter.getProperty("end") != null) {
            end = new Alpha(parameter.getProperty("end"));
        }
        pulsator = new Pulsator(parameter, nifty.getTimeProvider());
    }

    public void execute(
            final Element element,
            final float normalizedTime,
            final Falloff falloff,
            final NiftyRenderEngine r) {
        if (!activated && normalizedTime > 0.0f) {
            activated = true;
            pulsator.reset();
        }

        float value = pulsator.update();
        if (activated) {
            if (interpolator != null) {
                r.setColorAlpha(interpolator.getValue(value));
            } else {
                Alpha a = start.linear(end, value);
                r.setColorAlpha(a.getAlpha());
            }



        }
    }

    public void deactivate() {
        activated = true;
    }
}
